﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WishHand.Core;

namespace SuningProductTransfer
{
    public partial class 奥弋图片工具 : Form
    {
        public 奥弋图片工具()
        {
            InitializeComponent();
        }


        Dictionary<string, IList<string>> dic = new Dictionary<string, IList<string>>();
        private void ChooseExcelButton_Click(object sender, EventArgs e)
        {
            string filePath = "image/{0}/{1}/";
            string path = @"C:\Users\95\Documents\WeChat Files\wantaotao1994\Files\inputproduct.xlsx";
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "请选择文件";
            fileDialog.Filter = "所有文件(*xlsx*)|*.xlsx*"; //设置要选择的文件的类型
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                path = fileDialog.FileName;//返回文件的完整路径           

                richTextBox1.AppendText("已经选择文件:" + path);
            }
            else
            {
                return;
            }

            ExcelHelper excelHelper = new ExcelHelper();
            var list = excelHelper.ReadExcel(path);

            richTextBox1.AppendText("分析表格完成,总共:" + list.Count);

            IList<IList<string>> outList = new List<IList<string>>();


            for (int i = 0; i < list.Count; i++)
            {
                IList<string> outInnerList = new List<string>();

                for (int j = 0; j < 11; j++)
                {
                    outInnerList.Add("");
                }

                if (list[i].Count < 2)
                {
                    continue;
                }
                string spuId = list[i][1];
                string mainPic = list[i][7];
                string brand = list[i][8];
                string title = list[i][10];
                string detail = "";

                if (list[i].Count >= 13)
                {
                    detail = list[i][13];
                }


                string mainBasePath = string.Format(filePath, spuId, "ZT");

                outInnerList[0] = spuId;
                outInnerList[2] = brand;
                outInnerList[3] = title;

                outInnerList[5] = mainBasePath+ "1.jpg";
                outInnerList[6] = mainBasePath+ "1.jpg";
                outInnerList[7] = mainBasePath+ "1.jpg";
                outInnerList[8] = mainBasePath+ "1.jpg";
                outInnerList[9] = mainBasePath+ "1.jpg";
                if (!Directory.Exists(mainBasePath))
                {
                    Directory.CreateDirectory(mainBasePath);
                }
                ImageDownClient.DownLoadImage(mainPic, mainBasePath, "1.jpg");
                richTextBox1.AppendText($"下载完主图,本地地址:{mainBasePath + "1.jpg"} \n");
                var result = ProductHelper.GetOutsiteLinkImgs(detail);
                string detailBasePath = string.Format(filePath, spuId, "XQ");

                for (int k = 0; k < result.Count; k++)
                {
                    if (k > 19)
                    {
                        continue;
                    }
                    try
                    {

                        if (!Directory.Exists(detailBasePath))
                        {
                            Directory.CreateDirectory(detailBasePath);
                        }
                        var imageFale = ImageDownClient.DownLoadImage(result[k], detailBasePath, k + ".jpg", true);

                        if (imageFale.IsBig)
                        {
                            foreach (var item in imageFale.result)
                            {
                                outInnerList.Add(item);
                            }
                        }
                        else
                        {
                            outInnerList.Add(detailBasePath+k+".jpg");
                        }
                        richTextBox1.AppendText($"下载详情图:{spuId}_{k} \n");


                    }
                    catch (Exception ex)
                    {


                    }

                }

                outList.Add(outInnerList);
            }
            excelHelper.CreateExcel(outList, "result.xlsx");
            MessageBox.Show("完成");

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
